package gentle.controller;

import com.baomidou.mybatisplus.plugins.Page;
import gentle.base.BaseController;
import gentle.entity.CustomerInfo;
import gentle.entity.SearchCustomerInfo;
import gentle.factory.PageFactory;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import gentle.service.IScoreDetailService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

/**
 * 积分明细
 */
@Controller
@RequestMapping("/scoreDetail")
public class ScoreDetailController extends BaseController {
    @Autowired
    IScoreDetailService scoreDetailService;

    /**
     * 积分明细列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object testrInfoList(SearchCustomerInfo info, HttpServletRequest request) {
        if(info.getAbbreviation()=="" || info.getAbbreviation() ==null || info.getAbbreviation()=="null"){
            return null;
        }else{
            Page<CustomerInfo> page = new PageFactory<CustomerInfo>().defaultPage();
            try {
                info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
                info.setLimitIndex(page.getSize());
                page.setCurrent(0);
                List<CustomerInfo> list = scoreDetailService.selectPageListByQueryGetMap(page, info);
                if (list.size() == 0) {
                    info.setStartIndex(0);
                    list = scoreDetailService.selectPageListByQueryGetMap(page, info);
                }
                page.setTotal(scoreDetailService.getQueryPagesCount(page, info));
                page.setRecords(list);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return super.packForBT(page);
        }
    }

    @RequestMapping("/sumScoreDetail")
    @ResponseBody
    public HashMap sumScoreDetail(@RequestParam(required = false) String reach, @RequestParam(required = false) String customerStatus,@RequestParam(required = false) String fieldTc,
                                  @RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime,
                                  @RequestParam(required = false) String abbreviation,@RequestParam(required = false) String outpatientName) {
        return scoreDetailService.sumScoreDetail(reach, customerStatus,abbreviation,fieldTc, startTime, endTime,outpatientName);
    }

    @RequestMapping(value = "/scoreDetailExport")
    public void scoreDetailExport(@Valid SearchCustomerInfo searchInfo, HttpServletResponse response) {
        scoreDetailService.scoreDetailExport(searchInfo,response);
    }
}